import HttpUtil from '../../lib/trilobite/core/httputil.js'
//获取应用实例
var app = getApp();
let comp ,self;
//登陆类
class selfLoginDao {
  constructor() {
    this.http = new HttpUtil(app);
    this.http.addResultListener(this.result);
  }
  result = (res) => {
    if (this.callback) {
      this.callback(res);
    }
  }
  /**
   * 加载登录接口
   */
  load = () => {
    this.http.post("/EsMember/mjShopWxAutologin",
      {
        sex: app.globalData.userInfo.gender,
        city: app.globalData.userInfo.city,
        province: app.globalData.userInfo.province,
        nickname: app.globalData.userInfo.nickname,
        face: app.globalData.userInfo.avatarUrl,
        code: self.data.code,
        iv: app.globalData.userInfo.iv,
        encryptedData: app.globalData.userInfo.encryptedData
      })
      }

}
/**
 * 启动开始封面
 */
class PageController {
  constructor() {
    comp = this;
    comp.selfLoginDao = new selfLoginDao();
    comp.selfLoginDao.callback = this.selfLoginDao_callback;
  }
  
  data={
    remind: '加载中',
    angle: 0,
    userInfo: {}
  }
  // 渲染数据
 selfLoginDao_callback = (res) => {
   if (res.data.message == "ok") {
     //设置全局数据
     app.globalData.userInfo = res.data.data;
     app.globalData.userInfo.unionid = res.data.data.wxUnionid;
     console.log('登录成功 memberId = ' + app.globalData.userInfo.memberId);
     console.log('登录成功 wxUnionid = ' + app.globalData.userInfo.wxUnionid);
     console.log('登录成功 wxOpenid = ' + app.globalData.userInfo.wxOpenid);
   } else {
     setTimeout(function () {
       console.log('出错信息 = ' + res.data.message);
       that.move();
     }, 500)
   }
  }
  /**
   * 进入商城首页
   */
  goToIndex=()=>{
    var that = this;
    if (wx.getStorageSync("memberId")) {
    } else { that.move(); return }
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
  /**
   * 当页面加载的时候执行
   */
  onLoad=function() {
   self = this;

    var that = this

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          //必须先调用wx.login，再调用wx.getUserInfo，否则无法解密encryptedData
          wx.login({
            success: function (res) {
              console.log('获取到的code：' + res.code);
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
              that.setData({
                code: res.code
              })
              wx.getUserInfo({
                success: res => {
                  // 可以将 res 发送给后台解码出 unionId
                  console.log("授权后，取数据成功iv：" + res.iv);
                  app.globalData.userInfo = res.userInfo;
                  app.globalData.userInfo.encryptedData = res.encryptedData;
                  app.globalData.userInfo.iv = res.iv;
                  console.log('缓存用户授权信息成功');
                  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  if (this.userInfoReadyCallback) {
                    this.userInfoReadyCallback(res);
                  }
                  console.log('开始进行登录 code = ' + that.data.code);
                  comp.selfLoginDao.load();
                }
              })

            }
          });
        } else {
          //未授权
          setTimeout(function () {
            console.log('未授权，跳转页面去授权');
            that.move();
          }, 500)
        }
      }
    });
   
  }
 
  //切换到授权页面
  move = () =>{
    wx.navigateTo({
      url: '/pages/user_info/user_info',
    })
  }
 
  onShow=function() {

  }
  /**
   * 制作头像动画
   */
  onReady=function() {
    var that = this;
    setTimeout(function () {
      that.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function (res) {
      var angle = -(res.x * 30).toFixed(1);
      if (angle > 14) { angle = 14; }
      else if (angle < -14) { angle = -14; }
      if (that.data.angle !== angle) {
        that.setData({
          angle: angle
        });
      }
    });
  }
}

Page(new PageController());
